package com.bocloud.util;

import com.bocloud.model.Department;
import com.bocloud.model.Menu;
import com.bocloud.model.Role;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/12/21.
 */
public class TreeUtil {

    /**
     * 使用递归方法建树，做成角色的树形下拉框
     *
     * @param Roles
     * @return
     */
    public static List<Role> buildByRole(List<Role> Roles) {
        List<Role> trees = new ArrayList<Role>();
        for (Role Role : Roles) {
            if ("r0".equals(Role.getRoleId())) {
                trees.add(findroChildren(Role, Roles));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点，做成角色的树形下拉框
     *
     * @param Roles
     * @return
     */
    public static Role findroChildren(Role Role, List<Role> Roles) {
        Role.setChildren(new ArrayList<Role>());
        for (Role it : Roles) {
            if (Role.getRoleId().equals(it.getPid())) {
                if (Role.getChildren() == null) {
                    Role.setChildren(new ArrayList<Role>());
                }
                Role.getChildren().add(findroChildren(it, Roles));
            }
        }
        return Role;
    }

    /**
     * 使用递归方法建树，做成部门的树形下拉框
     *
     * @param Departments
     * @return
     */
    public static List<Department> buildByRecursive(List<Department> Departments) {
        List<Department> trees = new ArrayList<Department>();
        for (Department Department : Departments) {
            if ("0".equals(Department.getDepartmentId())) {
                trees.add(findChildren(Department, Departments));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点，做成部门的树形下拉框
     *
     * @param Departments
     * @return
     */
    public static Department findChildren(Department Department, List<Department> Departments) {
        Department.setChildren(new ArrayList<Department>());
        for (Department it : Departments) {
            if (Department.getDepartmentId().equals(it.getPid())) {
                if (Department.getChildren() == null) {
                    Department.setChildren(new ArrayList<Department>());
                }
                Department.getChildren().add(findChildren(it, Departments));
            }
        }
        return Department;
    }

    /**
     * 使用递归方法建树，做成菜单的树形下拉框
     *
     * @param Menus
     * @return
     */
    public static List<Menu> buildByMenu(List<Menu> Menus) {
        List<Menu> trees = new ArrayList<Menu>();
        for (Menu Menu : Menus) {
            if ("0".equals(Menu.getPid())) {
                trees.add(findmeChildren(Menu, Menus));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点，做成菜单的树形下拉框
     *
     * @param Menus
     * @return
     */
    public static Menu findmeChildren(Menu Menu, List<Menu> Menus) {
        Menu.setChildren(new ArrayList<Menu>());
        for (Menu it : Menus) {
            if (Menu.getMenuId().equals(it.getPid())) {
                if (Menu.getChildren() == null) {
                    Menu.setChildren(new ArrayList<Menu>());
                }
                Menu.getChildren().add(findmeChildren(it, Menus));
            }
        }
        return Menu;
    }

}
